// 用户信息管理模块
// 用户多组件共享  完整
// import { userAccountLogin } from '@/api/user'
import { userAccountLogin } from '@/api/user'
import router from '@/router'
import { Message } from 'element-ui'
export default {
  namespaced: true,
  state: {
    profile: {},
    token: ''
  },
  mutations: {
    setUseInfo(state, userInfo) {
      console.log(userInfo);
      state.profile = userInfo
      if (state.profile.city) {
        state.profile.city = state.profile.city.city_name
      }
    },
    // 清除操作
    clearUserInfo(state) {
      state.profile = {}
    },
    setToken(state, token) {
      state.token = token
    },
    // 退出登录
    deletetoken(state) {
      console.log(1);
      state.token = null
      state.profile = {}
    }
  },
  actions: {
    // 异步函数  调用登录接口
    async login(ctx, data) {
      console.log(data);
      const res = await userAccountLogin(data)
      if (res.data.statu == 200) {
        console.log(res.data);
        Message({
          message: '欢迎您！管理员',
          type: 'success'
        })
        ctx.commit('setToken', res.data.token)
        ctx.commit('setUseInfo', res.data.data)
        router.push('/')
      } else {
        Message('账号密码有误')
      }
    }
  }
}
